﻿@model TaskManager.Web.Controls.TabContainer
           
<script type="text/jscript">
    $(function () {
        $('.button-tab-close').click(function () {
            $('#closeTab').submit();
        });
    });
</script>


@helper PrintTab(TaskManager.Web.Controls.Tab tab, bool active)
{
    if(active)
    {
        <li class="active">
            <a href="@tab.Url">@tab.Text</a>

            @if(tab.Closable)
            {
                <span class="button-tab-close">&times;</span>
                
                using (Html.BeginForm("Close", "Tab", FormMethod.Post, new { id = "closeTab" }))
                {
                    @Html.Hidden("tabId", tab.TabId)
                }
            }
        </li>
    }
    else
    {
        <li>
            <a href="@tab.Url">@tab.Text</a>
        </li>
    }    
}

<ul class="tabs">
@{
    if (Model != null)
    {
        TaskManager.Web.Controls.Tab defaultTab = Model[0];

        if (Model.ActiveTab == defaultTab.TabId)
        {
            defaultTab.Text = @ViewBag.Title;
            defaultTab.Url = Request.RawUrl;
        }

        if (Request.IsAuthenticated)
        {
            foreach(TaskManager.Web.Controls.Tab tab in Model.Tabs)
            {
                @PrintTab(tab, (tab.TabId == Model.ActiveTab));
            }
        }
        else
        {
            @PrintTab(Model[0], true)
        }
    }
}
</ul>
